package gipf.trunk;
import java.util.ArrayList;


/**
 * @author eric
 *has an int[] which describes a current boardState
 *e.g. an empty board would just be an int[61] with only zeros.
 */
public class State {
	private int[] pieces;
	private ArrayList<position> lastMoves;
	
	
	/**
	 * create a new boardstate, which contains an int array.
	 * this array holds 0 for an empty spot, 1/2 for the respective players
	 * maybe also 11/12 could for example represent a gipf stone, so 
	 * determining a player would just be %10.
	 */
	public State(){
		pieces = new int[61];
	}
	
	/**
	 * @return an arraylist of integers, on which a piece is placed
	 * with this int value in the pieces array you can get the value
	 * of what is placed there
	 */
	public ArrayList<Integer> getPlacedPieces(){
		ArrayList<Integer> list = new ArrayList<Integer>();
		
		for (int i : pieces)
			if (pieces[i] != 0)
				list.add(i);
		
		return list;
	}
	
	/**
	 * @return the int array that represents a boardstate
	 */
	public int[] getPieces(){
		return pieces;
	}
	
	/**
	 * @param a ,the position where you want to insert a piece
	 * @param player ,the number representing the player placing the piece
	 * @param gipf ,if true not the number of the player is stored, but the number + 10
	 */
	public void placePiece(position a, int player, boolean gipf){
		int index = a.getIndex();
		
		if (pieces[index] == 0){
			if(gipf){
				pieces[index] = 10 + player;

			}
			else
				pieces[index] = player;
		}
		
		lastMoves = new ArrayList<position>();
	}
	
	/**
	 * @param a ,the location of the piece you want to move into
	 * @param direction ,into which you want to shift the certain piece (check for full line before!)
	 */
	public void movePiece(position a, int direction){
		
		position nextP = staticBoard.getAdjecent(a, direction);
		int next = nextP.getIndex();
		
		lastMoves.add(nextP);

		
		if (pieces[next] == 0){
			pieces[next] = pieces[a.getIndex()];
			pieces[a.getIndex()] = 0;
		}
		else{
			movePiece(nextP, direction);
			movePiece(a, direction);
		}
	}
	
	/**
	 * @return the arraylist containing int's that describe the pieces that were last moved
	 */
	public ArrayList<position> getLastMoves(){
		return lastMoves;
	}
	
	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	public String toString(){
		String toBeReturned = "";
		
		for (int i = 0; i < pieces.length; i++){
			if (pieces[i] != 0)
				toBeReturned += (i + " " + pieces[i] + "\n");
		}
		
		return toBeReturned;
	}

}
